cmake_minimum_required(VERSION 3.8)

project(grpc_ex CXX)

get_filename_component(ex_proto "grpc_ex.proto" ABSOLUTE)
get_filename_component(ex_proto_path "${ex_proto}" PATH)

# Generated sources
set(ex_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/../grpc_ex.pb.cc")
set(ex_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/../grpc_ex.pb.h")
set(ex_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/../grpc_ex.grpc.pb.cc")
set(ex_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/../grpc_ex.grpc.pb.h")

#[[
find_package(Protobuf REQUIRED)
add_custom_command(
      OUTPUT "${ex_proto_srcs}" "${ex_proto_hdrs}" "${ex_grpc_srcs}" "${ex_grpc_hdrs}"
      COMMAND ${_PROTOBUF_PROTOC}
      ARGS --grpc_out="${CMAKE_CURRENT_BINARY_DIR}/.."
        --cpp_out="${CMAKE_CURRENT_BINARY_DIR}/.."
        -I "${ex_proto_path}/.."
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        "${ex_proto}"
      DEPENDS "${ex_proto}")
]]
# Include generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}/..")

find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")

find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")

set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
set(_REFLECTION gRPC::grpc++_reflection)
set(_GRPC_GRPCPP gRPC::grpc++)

# ex_grpc_proto
add_library(ex_grpc_proto
  ${ex_grpc_srcs}
  ${ex_grpc_hdrs}
  ${ex_proto_srcs}
  ${ex_proto_hdrs})
target_link_libraries(ex_grpc_proto
  ${_REFLECTION}
  ${_GRPC_GRPCPP}
  ${_PROTOBUF_LIBPROTOBUF})


add_executable(server "server.cpp")
target_link_libraries(server
  ex_grpc_proto
  ${_REFLECTION}
  ${_GRPC_GRPCPP}
  ${_PROTOBUF_LIBPROTOBUF})

add_executable(client "client.cpp")
target_link_libraries(client
  ex_grpc_proto
  ${_REFLECTION}
  ${_GRPC_GRPCPP}
  ${_PROTOBUF_LIBPROTOBUF})
